package spindles.api.domain;

import java.io.Serializable;
import java.util.Comparator;
import java.util.Date;

import org.apache.commons.collections.comparators.ReverseComparator;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import com.sleepycat.persist.model.Entity;

@Entity
public class Audit extends DomainObject {

	/**
	 * 
	 */
	private static transient final long serialVersionUID = -2913288928723683861L;
	
	private String filename;
	
	private Date importStartTime;
	
	private Date importEndTime;
	
	private int fileSize; // in MB
	
	private int recordsCount;	
	
	private int eegDuration; //in seconds
	
	private int samplingRate;	
	
	
	public Audit(){}


	public int getEegDuration() {
		return eegDuration;
	}


	public void setEegDuration(int eegDuration) {
		this.eegDuration = eegDuration;
	}


	public String getFilename() {
		return filename;
	}


	public void setFilename(String filename) {
		this.filename = filename;
	}


	public int getFileSize() {
		return fileSize;
	}


	public void setFileSize(int fileSize) {
		this.fileSize = fileSize;
	}
	
	public int getImportDuration(){
		return 
		new Interval(getImportStartTime(), getImportEndTime()).duration().intValue();
	}


	public Date getImportEndTime() {
		return importEndTime;
	}


	public void setImportEndTime(Date importEndTime) {
		this.importEndTime = importEndTime;
	}


	public Date getImportStartTime() {
		return importStartTime;
	}


	public void setImportStartTime(Date importStartTime) {
		this.importStartTime = importStartTime;
	}


	public int getRecordsCount() {
		return recordsCount;
	}


	public void setRecordsCount(int recordsCount) {
		this.recordsCount = recordsCount;
	}


	public int getSamplingRate() {
		return samplingRate;
	}


	public void setSamplingRate(int samplingRate) {
		this.samplingRate = samplingRate;
	}
	
	public boolean equals(Object obj){
		return EqualsBuilder.reflectionEquals(this, obj, getExcludedFields());
	}
	
	public int hashCode(){
		return HashCodeBuilder.reflectionHashCode(this, getExcludedFields());
	}
	
	public int compare(Object o){
		return CompareToBuilder.reflectionCompare(this, o, getExcludedFields());
	}
	
	private static String[] getExcludedFields(){
		return new String[]{"id", "eegDuration", "filename", "fileSize", 
				"importStartTime", "recordsCount", "samplingRate"};
	}
	
	public static Comparator getComparator(){
		return new ReverseComparator(new AuditComparator());
	}
	
	private static class AuditComparator implements Comparator, Serializable{

		public int compare(Object o1, Object o2) {			
			return CompareToBuilder.reflectionCompare(o1, o2, getExcludedFields());
		}
		
	}
	
	

}
